/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.mes.quality.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.mes.quality.entity.AnalysisProcessParam;
import org.springblade.modules.mes.quality.mapper.AnalysisProcessParamMapper;
import org.springblade.modules.mes.quality.service.IAnalysisProcessParamService;
import org.springblade.modules.mes.quality.utils.CalSta;
import org.springblade.modules.mes.quality.vo.AnalysisProcessParamVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 质量分析-总成加工参数 服务实现类
 *
 * @author liuchao
 * @since 2023-02-20
 */
@Service
//@DS(value = "slave")
public class AnalysisProcessParamServiceImpl extends BaseServiceImpl<AnalysisProcessParamMapper, AnalysisProcessParam> implements IAnalysisProcessParamService {

    @Override
    public IPage<AnalysisProcessParamVO> selectAnalysisProcessParamPage(IPage<AnalysisProcessParamVO> page, AnalysisProcessParamVO analysisProcessParam) {
        return page.setRecords(baseMapper.selectAnalysisProcessParamPage(page, analysisProcessParam));
    }

    @Override
    public R<List<String>> getAssemblyBarcodeList(String assemblyBarcode, String type) {
        return R.data(baseMapper.selectAssemblyBarcodeList(assemblyBarcode, type));
    }

    @Override
    public R<Map<String, Object>> getHistogram(AnalysisProcessParamVO analysisProcessParam) {
        Map<String, Object> map = new HashMap<>();
        List<AnalysisProcessParamVO> analysisProcessParamArrayList;
        if(StringUtil.isNotBlank(analysisProcessParam.getIds())){
            analysisProcessParamArrayList = baseMapper.selectByIdListType(Func.toLongList(analysisProcessParam.getIds()), analysisProcessParam.getType());
        }else{
            analysisProcessParamArrayList=baseMapper.selectAnalysisProcessParamPage(null, analysisProcessParam);
        }
        if (analysisProcessParamArrayList.size() > 300) {
            return R.fail("内存不足，曲线分析不能超过300条数据");
        }
        List<String> strList1 = new ArrayList<>();
        if ("2".equals(analysisProcessParam.getType()) || "3".equals(analysisProcessParam.getType())) {
            strList1 = analysisProcessParamArrayList.stream().map(AnalysisProcessParam::getData2).collect(Collectors.toList());
        }
        Map<String, Object> map1= CalSta.getNormal(strList1);
        map.put("map1", map1);
        List<String[]> minList = new ArrayList<>();
        List<String[]> zzList = new ArrayList<>();
        List<String[]> maxList = new ArrayList<>();
        for (int i = 0, length = analysisProcessParamArrayList.size(); i < length; i++) {
            String index=String.valueOf(i + 1);
            String assemblyBarcode=analysisProcessParamArrayList.get(i).getAssemblyBarcode();
            String[] a = new String[3];
            a[0] = index;
            a[1] = analysisProcessParamArrayList.get(i).getData2();
            a[2] = assemblyBarcode;
            zzList.add(a);
            String[] b = new String[3];
            b[0] = index;
            b[1] = analysisProcessParamArrayList.get(i).getData1();
            b[2] = assemblyBarcode;
            minList.add(b);
            String[] c = new String[3];
            c[0] = index;
            c[1] = analysisProcessParamArrayList.get(i).getData3();
            c[2] = assemblyBarcode;
            maxList.add(c);
        }
        Map<String,Object> map2=new HashMap<>();
        map2.put("minList",minList);
        map2.put("zzList",zzList);
        map2.put("maxList",maxList);
        map.put("map2",map2);
        return R.data(map);
    }


}
